<?php
namespace app\action\admin;
use core\BaseAction;
use core\App;
use core\JsonInfo;
use core\ParamFilter;
//文章管理
class ArticleAction extends BaseAction{
	//加载文章列表页面
	function getList($params,$req,$res){
		return $this->view("admin.article_list");
	}
	//加载文章列表数据
	function postList($params){
		$service = App::bean('\\app\\service\\admin\\ArticleService');
		$filter = new ParamFilter($params);
		
		$table = $service->table()->select("title","create_time","edit_time","id","user_name","status","orders");
		
		//非管理员，只查询自己的文章
		$user = $user = App::getCurrentUser();
		if($user!=null&&$user->is_admin!=1){
			$filter->addFilter('Q_user_id_=',$user->id);
		}
		$service->queryByFilter($filter,$table);
		$this->json($filter->grid);
	}
	//加载文章添加页面
	function getAdd(){
		return $this->view("admin.article_add");
	}
	//文章添加或保存
	function postAdd($params){
		$info = new JsonInfo();
		//管理员才可以更新发布状态
		if($_SESSION['user']->is_admin==1){
			if($params['status']){
				$params['status'] = 1;
			}else{
				$params['status'] = 0;
			}
		}else{
			$params['status'] = 0;
		}
		
		
		if(empty($params['title'])){
			$info->msg = "非法数据";
		}else{
			$service = App::bean('\\app\\service\\admin\\ArticleService');
			$service->saveArticle($params,$info);
		}
		return $this->json($info);
	}
	
	//获取一篇文章的数据
	function postGet($params){
		if(empty($params['id'])){
			$info->msg = "非法数据";
		}else{
			$service = App::bean('\\app\\service\\admin\\ArticleService');
			$data = $service->getFirst("id",$params['id']);
		}
		return $this->json($data);
	}
	
	//删除文章
	function postDelete($params){
		$info = new JsonInfo();
		if(empty($params['Q_id_in'])){
			$info->msg = "非法数据";
		}else{
			$service = App::bean('\\app\\service\\admin\\ArticleService');
			$filter = new ParamFilter($params);
			//非管理员，只能删除自己的数据
			if($_SESSION['user']->is_admin==0){
				$filter->addFilter('Q_user_id_=',$_SESSION['user']->id);
			}
			$info = $service->deleteByFilter($filter);
		}
		return $this->json($info);
	}
}